****** Factor Measures prepared for export to Matlab for analysis


cd ""
clear 
set more off

use survey_vars



******************* Reducing dimensionality to lower number of parameters to be estimated

********************** 1.Emotional Stability




local cat stable8-stable11
foreach var of varlist `cat'{
	gen b_`var'=`var'>3
	}
drop `cat'

local cat de_grat2  
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}

local cat posit8
foreach var of varlist `cat' {
	gen b_`var'=`var'>2
	}	
drop `cat'

local cat posit9  
foreach var of varlist `cat' {
	gen b_`var'=`var'>2
	}	
drop posit9 posit10

	
********************** 2.Cognitive
gen temp=inlist(cognit2,1,2,3)
replace temp=2 if inlist(cognit2,4,5)
replace temp=3 if inlist(cognit2,6)
replace temp=4 if inlist(cognit2,7)
tab temp cognit2
drop cognit2
rename temp cognit2

********************** 3.Extraversion
local cat confident* cre4

foreach var of varlist `cat' {
	gen b_`var'=`var'>2
	}
drop `cat'

local cat ex9
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}
drop `cat'



local cat rebel3 
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}
drop `cat'




********************** 4.Conscientiousness

local cat dilligent1 dilligent3 
foreach var of varlist `cat' {
	gen b_`var'=`var'>3
	}


local cat dilligent5 dilligent8 dilligent10 dilligent11 dilligent12      
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}


drop dilligent1 dilligent3 dilligent5 dilligent8 dilligent10 dilligent11 dilligent12   


foreach var of varlist dilligent* {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3,4,5)
	replace temp=3 if inlist(`var',6,7)
	tab temp `var'
	drop `var'
	rename temp `var'
	}


local cat de_grat4 de_grat5 de_grat6 de_grat7 de_grat10 de_grat11  
foreach var of varlist `cat' {
	gen b_`var'=`var'>4
	}

drop de_grat4 de_grat5 de_grat6 de_grat7 de_grat10 de_grat11 de_grat2


foreach var of varlist de_grat* {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3,4,5)
	replace temp=3 if inlist(`var',6,7)
	tab temp `var'
	drop `var'
	rename temp `var'
	}




foreach var of varlist cre6 {
	gen temp=inlist(`var',1,2)
	replace temp=2 if inlist(`var',3)
	replace temp=3 if inlist(`var',4,5)
	tab temp `var'
	drop `var'
	rename temp `var'
	}
	
foreach var of varlist cre10  {
	gen temp=inlist(`var',1)
	replace temp=2 if inlist(`var',2,3)
	replace temp=3 if inlist(`var',4)
	replace temp=4 if inlist(`var',5,6)	
	replace temp=5 if inlist(`var',7)		
	tab temp `var'
	drop `var'
	rename temp `var'
	}
	


local cat posit3-posit5 
foreach var of varlist `cat'{
	gen b_`var'=`var'==4
	}
drop `cat'








************************************* Now need to get in correct order for matlab estimator: organize by factor (binary, multi, continous in this order)

global id "subject"
*** sex=1=female
*** english=1=home language is english
*** age1=1=17
*** age2=1=18
*** age3=1=19+

global dems "sex english age17 age18 age19plus"
global es "stable1-stable5 stable7 b_stable8 b_posit9 b_de_grat2 b_posit8"
global cognit "cognit*"
global extrav "b_confident2 ex3 ex4 b_confident6 b_confident3 ex7 ex8 b_ex9 b_rebel3 b_cre4"
global consci "b_dilligent5 dilligent6 dilligent9 b_dilligent10 b_dilligent12 de_grat12 b_de_grat7 cre6 de_grat8 b_dilligent11"


local j=0
foreach cat in dems es cognit extrav consci  {
	local i=0
	local k=0
	local l=0
	foreach var of varlist $`cat' {
		quietly sum `var'
		*binary
		if `r(max)'==1 {
			local ++i
			* keeps track of which dummy became what var
			di "`var' _`j'`cat'_1b_`i'"
			rename `var' _`j'`cat'_1b_`i'
			}
		*multi-valued
		else if `r(max)'<=5 {
			* so that the 1st cat is at 0 - thats what the estimation code needs as input
			replace `var'=`var'-1
			local ++k
			rename `var' _`j'`cat'_2m_`k'
			di "`var' _`j'`cat'_2m_`k'"
			}	
		*continuous
		else {
			local ++l
			* standardizes the continuous variables to be on a similar scale as the rest
			* now is mean 0, std 1 
			replace `var' = (`var' - `r(mean)')/`r(sd)'
			rename `var' _`j'`cat'_3c_`l'
			di "var' _`j'`cat'_3c_`l'"
			}
		}
	di "`cat'"
	* counts the number of binary, multi-valued, and continuous indicators in each factor
	di "`i' `k' `l'"
	di `i'+`k'+`l'
	local ++j
	}

order  _all, sequential

* drops the unused variables
keep _*

outsheet using factor_data_base, comma replace
save factor_data_base, replace







